package com.zt.questionnaire.common.utils;

import java.util.*;
import java.util.Map.Entry;

public class SortUtils {

    /**
     * 将map(或json对象)按照字段名的ASCII 码从小到大排序（字典序）后，使用URL键值对的格式（即key1=value1&key2=value2…）拼接成字符串string1
     *
     * @param map
     * @return
     */
    public static String sortMapToStr(Map<String, Object> map) {
        if (map == null || map.size() == 0) {
            return null;
        }
        ArrayList<String> list = new ArrayList<String>();
        Set<Entry<String, Object>> entrySet = map.entrySet();
        for (Entry<String, Object> entry : entrySet) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (value != null && !"".equals(value)) {
                list.add(key + "=" + value + "&");
            }
        }
        return sort(list);
    }

    public static String sortMapToStr1(Map<String, String> map) {
        if (map == null || map.size() == 0) {
            return null;
        }
        ArrayList<String> list = new ArrayList<String>();
        Set<Entry<String, String>> entrySet = map.entrySet();
        for (Entry<String, String> entry : entrySet) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (value != null && !"".equals(value)) {
                list.add(key + "=" + value + "&");
            }
        }
        return sort(list);
    }

    private static String sort(ArrayList<String> list) {
        int size = list.size();
        if (size == 0) {
            return null;
        }
        Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append(list.get(i));
        }
        //去掉字符串末尾的"&"
        String result = sb.toString();
        result = result.substring(0, result.length() - 1);
        return result;
    }

    /**
     * 排序
     *
     * @param map
     * @param sort 0=升序，1=降序
     * @return
     */
    public static List<Entry<Integer, Integer>> sortMapValue(Map<Integer, Integer> map, int sort) {
//        Map<String, String> map = new HashMap<String, String>();
//        map.put("c", "ccccc");
//        map.put("a", "aaaaa");
//        map.put("b", "bbbbb");
//        map.put("d", "ddddd");
        if (map == null || map.size() == 0) {
            return null;
        }
        Set<Entry<Integer, Integer>> entrySet = map.entrySet();
        List<Entry<Integer, Integer>> list = new ArrayList<>(entrySet);
        Collections.sort(list, new Comparator<Entry<Integer, Integer>>() {
            //升序排序
            public int compare(Entry<Integer, Integer> o1,
                               Entry<Integer, Integer> o2) {
                if (sort == 0) {
                    return o1.getValue().compareTo(o2.getValue());
                } else {
                    return o2.getValue().compareTo(o1.getValue());
                }
            }

        });

//        for(Map.Entry<Integer,Integer> mapping:list){
//               System.out.println(mapping.getKey()+":"+mapping.getValue());
//          }
        return list;
    }

    public static List<Entry<Integer, Float>> sortMapValue2(Map<Integer, Float> map, int sort) {
//      Map<String, String> map = new HashMap<String, String>();
//      map.put("c", "ccccc");
//      map.put("a", "aaaaa");
//      map.put("b", "bbbbb");
//      map.put("d", "ddddd");
        if (map == null || map.size() == 0) {
            return null;
        }
        Set<Entry<Integer, Float>> entrySet = map.entrySet();
        List<Entry<Integer, Float>> list = new ArrayList<>(entrySet);
        Collections.sort(list, new Comparator<Entry<Integer, Float>>() {
            //升序排序
            public int compare(Entry<Integer, Float> o1,
                               Entry<Integer, Float> o2) {
                if (sort == 0) {
                    return o1.getValue().compareTo(o2.getValue());
                } else {
                    return o2.getValue().compareTo(o1.getValue());
                }
            }

        });

//      for(Map.Entry<Integer,Integer> mapping:list){
//             System.out.println(mapping.getKey()+":"+mapping.getValue());
//        }
        return list;
    }

    /**
     * 排序
     *
     * @param map
     * @param sort 0=升序，1=降序
     * @return
     */
    public static List<Entry<String, Integer>> sortMapValue1(Map<String, Integer> map, int sort) {
//      Map<String, String> map = new HashMap<String, String>();
//      map.put("c", "ccccc");
//      map.put("a", "aaaaa");
//      map.put("b", "bbbbb");
//      map.put("d", "ddddd");
        if (map == null || map.size() == 0) {
            return null;
        }
        Set<Entry<String, Integer>> entrySet = map.entrySet();
        List<Entry<String, Integer>> list = new ArrayList<>(entrySet);
        Collections.sort(list, new Comparator<Entry<String, Integer>>() {
            //升序排序
            public int compare(Entry<String, Integer> o1,
                               Entry<String, Integer> o2) {
                if (sort == 0) {
                    return o1.getValue().compareTo(o2.getValue());
                } else {
                    return o2.getValue().compareTo(o1.getValue());
                }
            }

        });

//      for(Map.Entry<Integer,Integer> mapping:list){
//             System.out.println(mapping.getKey()+":"+mapping.getValue());
//        }
        return list;
    }
}
